package com.ysq.tankgame;

import java.util.Vector;

import com.ysq.tankgame.tankValue.Node;

/* 
 * 敌人的坦克
 */

public class EnteryTank extends MyTank implements Runnable {
    Vector<Shot> shots = null;
    Vector<EnteryTank> enemyTanks = null;

    boolean isLive = true;

    public boolean isLive() {
        return isLive;
    }

    public void setEnemyTanks(Vector<EnteryTank> enemyTanks) {
        this.enemyTanks = enemyTanks;
    }

    public void setLive(boolean isLive) {
        this.isLive = isLive;
    }

    public EnteryTank(int x, int y) {
        super(x, y);
        shots = new Vector<>();
    }

    public Vector<Shot> getShots() {
        return shots;
    }

    public void setShots(Shot shot) {
        shots.add(shot);
    }

    @Override
    public void run() {

        while (true) {
            if (shots.size() < 2) {
                Shot shot = null;
                switch (getDirect()) {
                    case 0: // 向上
                        shot = new Shot(getX() + 15, getY(), getDirect());
                        shots.add(shot);
                        new Thread(shot).start();
                        break;
                    case 1: // 向右
                        shot = new Shot(getX() + 60, getY() + 15, getDirect());
                        shots.add(shot);
                        new Thread(shot).start();
                        break;
                    case 2: // 向下
                        shot = new Shot(getX() + 15, getY() + 60, getDirect());
                        shots.add(shot);
                        new Thread(shot).start();
                        break;
                    case 3: // 向左
                        shot = new Shot(getX(), getY() + 15, getDirect());
                        shots.add(shot);
                        new Thread(shot).start();
                        break;
                }
            }

            switch (getDirect()) {
                case 0: // 向上
                    for (int i = 0; i < 10; i++) {
                        if (getY() > 0 && !isTouch()) {
                            moveUp();
                        } else {
                            setDirect((int) (Math.random() * 4));
                            break;
                        }
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1: // 向右
                    for (int i = 0; i < 10; i++) {
                        if (getX() + 60 < 1000 && !isTouch()) {
                            moveRight();
                        } else {
                            setDirect((int) (Math.random() * 4));
                            break;
                        }

                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2: // 向下
                    for (int i = 0; i < 10; i++) {
                        if (getY() + 60 < 750 && !isTouch()) {
                            moveDown();
                        } else {
                            setDirect((int) (Math.random() * 4));
                            break;
                        }
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3: // 向左
                    for (int i = 0; i < 10; i++) {
                        if (getX() > 0 && !isTouch()) {
                            moveLeft();
                        } else {
                            setDirect((int) (Math.random() * 4));
                            break;
                        }
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    break;
                default:
                    break;
            }

            setDirect((int) (Math.random() * 4));

            if (!isLive) {
                break;
            }

        }
    }

    /*
     * 判断是否碰撞
     * 
     */
    public Boolean isTouch() {
        switch (this.getDirect()) {
            case 0: // 朝上
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnteryTank eT = enemyTanks.get(i);
                    if (eT != this) {
                        // 敌方坦克上下朝向
                        if (eT.getDirect() == 0 || eT.getDirect() == 2) {
                            // 判断左上角 [this.getX(), this.getY(), this.getX() + 40, this.getY()]
                            if (this.getX() >= eT.getX() && this.getX() <= eT.getX() + 40 && this.getY() >= eT.getY()
                                    && this.getY() <= eT.getY() + 60) {
                                return true;
                            }
                            // 判断右上角
                            if (this.getX() + 40 >= eT.getX() && this.getX() + 40 <= eT.getX() + 40
                                    && this.getY() >= eT.getY() && this.getY() <= eT.getY() + 60) {
                                return true;
                            }

                        }
                        // 敌方坦克左右朝向
                        if (eT.getDirect() == 1 || eT.getDirect() == 3) {
                            // 判断左上角
                            if (this.getX() >= eT.getX() && this.getX() <= eT.getX() + 60
                                    && this.getY() >= eT.getY() && this.getY() <= eT.getY() + 40) {
                                return true;
                            }
                            // 判断右上角
                            if (this.getX() + 40 >= eT.getX() && this.getX() + 40 <= eT.getX() + 60
                                    && this.getY() >= eT.getY() && this.getY() <= eT.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 1: // 向右
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnteryTank eT = enemyTanks.get(i);
                    if (eT != this) {
                        // 敌方坦克上下朝向
                        if (eT.getDirect() == 0 || eT.getDirect() == 2) {
                            // 判断左上角 [this.getX(), this.getY(), this.getX() + 40, this.getY()]
                            if (this.getX() + 60 >= eT.getX() && this.getX() + 60 <= eT.getX() + 40
                                    && this.getY() >= eT.getY()
                                    && this.getY() <= eT.getY() + 60) {
                                return true;
                            }
                            // 判断右上角
                            if (this.getX() + 60 >= eT.getX() && this.getX() + 60 <= eT.getX() + 40
                                    && this.getY() + 40 >= eT.getY() && this.getY() + 40 <= eT.getY() + 60) {
                                return true;
                            }

                        }
                        // 敌方坦克左右朝向
                        if (eT.getDirect() == 1 || eT.getDirect() == 3) {
                            // 判断左上角
                            if (this.getX() + 60 >= eT.getX() && this.getX() + 60 <= eT.getX() + 60
                                    && this.getY() >= eT.getY() && this.getY() <= eT.getY() + 40) {
                                return true;
                            }
                            // 判断右上角
                            if (this.getX() + 60 >= eT.getX() && this.getX() + 60 <= eT.getX() + 60
                                    && this.getY() + 40 >= eT.getY() && this.getY() + 40 <= eT.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 2: // 向下
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnteryTank eT = enemyTanks.get(i);
                    if (eT != this) {
                        // 敌方坦克上下朝向
                        if (eT.getDirect() == 0 || eT.getDirect() == 2) {
                            // 判断左上角 [this.getX(), this.getY(), this.getX() + 40, this.getY()]
                            if (this.getX() >= eT.getX() && this.getX() <= eT.getX() + 40
                                    && this.getY() + 60 >= eT.getY()
                                    && this.getY() + 60 <= eT.getY() + 60) {
                                return true;
                            }
                            // 判断右上角
                            if (this.getX() + 40 >= eT.getX() && this.getX() + 40 <= eT.getX() + 40
                                    && this.getY() + 60 >= eT.getY() && this.getY() + 60 <= eT.getY() + 60) {
                                return true;
                            }

                        }
                        // 敌方坦克左右朝向
                        if (eT.getDirect() == 1 || eT.getDirect() == 3) {
                            // 判断左上角
                            if (this.getX() >= eT.getX() && this.getX() <= eT.getX() + 60
                                    && this.getY() + 60 >= eT.getY() && this.getY() + 60 <= eT.getY() + 40) {
                                return true;
                            }
                            // 判断右上角
                            if (this.getX() + 40 >= eT.getX() && this.getX() + 40 <= eT.getX() + 60
                                    && this.getY() + 60 >= eT.getY() && this.getY() + 60 <= eT.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 3: // 向左
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnteryTank eT = enemyTanks.get(i);
                    if (eT != this) {
                        // 敌方坦克上下朝向
                        if (eT.getDirect() == 0 || eT.getDirect() == 2) {
                            // 判断左上角 [this.getX(), this.getY(), this.getX() + 40, this.getY()]
                            if (this.getX() >= eT.getX() && this.getX() <= eT.getX() + 40
                                    && this.getY() + 40 >= eT.getY() + 40
                                    && this.getY() + 40 <= eT.getY() + 60) {
                                return true;
                            }
                            // 判断右上角
                            if (this.getX() >= eT.getX() && this.getX() <= eT.getX() + 40
                                    && this.getY() >= eT.getY() && this.getY() <= eT.getY() + 60) {
                                return true;
                            }

                        }
                        // 敌方坦克左右朝向
                        if (eT.getDirect() == 1 || eT.getDirect() == 3) {
                            // 判断左上角
                            if (this.getX() >= eT.getX() && this.getX() <= eT.getX() + 40
                                    && this.getY() + 40 >= eT.getY() && this.getY() + 40 <= eT.getY() + 40) {
                                return true;
                            }
                            // 判断右上角
                            if (this.getX() >= eT.getX() && this.getX() <= eT.getX() + 40
                                    && this.getY() >= eT.getY() && this.getY() <= eT.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            default:
                break;
        }
        return false;
    }
}
